#include <my_head.h>
typedef struct _Node
{
    int data;
    struct _Node *next;
    
}node_t;

int creat_node(node_t **p,int num){
    if(NULL == p){
        return -1;
    }
    *p = (node_t *) malloc(sizeof(node_t));
    if(NULL == *p){
        return -1;
    }
    (*p)->data = num;
    (*p)->next = NULL;
    return 0;
}

int print_list(node_t *phead){
    if(NULL == phead){
        return -1;
    }
    node_t *ptemp = phead->next;
    while(NULL != ptemp){
        printf("%d ",ptemp->data);
        ptemp = ptemp->next;
    }
    printf("\n");
    return 0;
}

int insert_list_by_head(node_t *phead,int num){
    if(NULL == phead){
        return -1;
    }
    node_t *pnew = NULL;
    creat_node(&pnew,num);
    pnew->next = phead->next;
    phead->next = pnew;
    return 0;
}

int insert_list_by_tail(node_t *phead,int num){
    if(NULL == phead){
        return -1;
    }
    node_t *ptemp = phead;
    while(NULL != ptemp->next)
    {
        ptemp = ptemp->next;
    }
    node_t *pnew = NULL;
    creat_node(&pnew,num);
    ptemp->next = pnew;
    return 0;
}

int insert_list_by_position(node_t *phead,int position,int num){
    if(NULL == phead){
        return -1;
    }
    if(position < 0){
        return -1;
    }
    node_t *ptemp = phead;
    int i = 0;
    for(i = 0;i<position;i++){
        if(NULL == ptemp->next){
            break;
        }
        ptemp = ptemp->next;
    }
    node_t *pnew = NULL;
    creat_node(&pnew,num);
    pnew->next = ptemp->next;
    ptemp->next = pnew;
    return 0;
}

int delete_list_by_head(node_t *phead){
    if(NULL == phead){
        return -1;
    }
    if(NULL == phead->next){
        return -1;
    }
    node_t *pdel = phead->next;
    phead->next = pdel->next;
    free(pdel);
    pdel = NULL;
    return 0;
}

int delete_list_by_tail(node_t *phead){
    if(NULL == phead){
        return -1;
    }
    if(NULL == phead->next){
        return -1;
    }
    node_t *ptemp = phead;
    while(NULL != ptemp->next->next){
        ptemp = ptemp->next;
    }
    free(ptemp->next);
    ptemp->next = NULL;
    return 0;
}

int modify_list_by_position(node_t *phead,int position,int new_num){
    if(NULL == phead){
        return -1;
    }
    if(NULL == phead->next){
        return -1;
    }
    if(position < 0){
        return -1;
    }
    node_t *ptemp = phead;
    int i = 0;
    for(i = 0;i<position;i++){
        if(NULL == ptemp->next->next){
            return -1;
        }
        ptemp = ptemp->next;
    }
    ptemp->next->data = new_num;
    return 0;
}

int tichong_list(node_t *phead){
    if(NULL == phead){
        return -1;
    }
    if(NULL == phead->next){
        return -1;
    }
    if(NULL == phead->next->next){
        return -1;
    }
    node_t *p = phead->next;
    node_t *m = NULL;
    node_t *q = NULL;
    while (NULL != p)
    {
        m = p;
        q = p->next;
        while(NULL != q){
            if(p->data != q->data){
                m = q;
                q = q->next;
            }else{
                m->next = q->next;
                free(q);
                q = m->next;
            }
        }
        p = p->next;
    }
    return 0;
    
}

int clean_list(node_t *phead){
    if(NULL == phead){
        return -1;
    }
    node_t *pdel = phead->next;
    while(NULL != phead->next){
        phead->next = pdel->next;
        free(pdel);
        pdel = phead->next;
    }
    pdel = NULL;
    return 0;
}


int main(int argc, char const *argv[])
{
    node_t *phead = NULL;
    creat_node(&phead,-1);
    insert_list_by_head(phead,10);
    insert_list_by_head(phead,20);
    insert_list_by_head(phead,40);
    insert_list_by_head(phead,50);
    insert_list_by_head(phead,30);
    print_list(phead);

    insert_list_by_tail(phead,60);
    insert_list_by_tail(phead,80);
    print_list(phead);

    insert_list_by_position(phead,2,20);
    insert_list_by_position(phead,4,50);
    insert_list_by_position(phead,8,20);//error
    print_list(phead);

    delete_list_by_head(phead);
    delete_list_by_head(phead);
    print_list(phead);

    delete_list_by_tail(phead);
    print_list(phead);

    modify_list_by_position(phead,2,10);
    print_list(phead);

    tichong_list(phead);
    print_list(phead);

    clean_list(phead);
    print_list(phead);

    return 0;
}
